 /*
  *@(#)IaaUtil.java		8 Sept. 2000
  *
  * Copyright: MetaPlus Inc & Inquiry Management Systems Ltd.  2000
  *
  * This software is the proprietary information of MetaPlus Inc & 
  * Inquiry Management Systems Ltd.  
  * Use is subject to license terms.
  */
package com.ims.iaa;

import java.rmi.*;
import java.io.*;
import java.util.*;

/**
  * This class <code>IaaUtil</code> provides several useful method for lookup
  * remote service. Service object include "Spider Control Center", "Request Control Center" 
  * and "Spider Database".
  *
  * @author Anthony Zhang
  *
  * @version 1.00, 25 Sept. 2000
  *
  */
public class IaaUtil {

	public static LogWriter out;

	/**
	  * Here the <code>LogWriter out</code> will only successfully 
	  * initailized for client side.
	  * For server side, it is not initialized until <code>IaaServer</code> create the
	  * the <code>RemoteLogWriterImpl</code> object.
	  */	
	static  {
		out = IaaUtil.getLogWriter(IaaDebug.LogWriteToScreen);	
	} 
	

	/**
	  * Lookup the Spider Control Center service.
	  *
	  * @params hostName The host name or IP address where the service located
	  * @params registryPort The port listened by the RMI registry ( By default
	  * RMI use port 1099 as the listen port).
	  * 
	  * @return The Spider Control Center service object( <code>SpiderCtl</code>
	  * object), or null if lookup failed.
	  *
	  * @see SpiderCtl
	  * @see SpiderCtlImpl
	  * @see IaaServer
	  */	
	public static SpiderCtl getSpiderCenter(String hostName, int registryPort) 
	{
            SpiderCtl adCenter = null;
			
       		String  serverName = "rmi://" + hostName + ":" + registryPort
         			+ "/SpiderCtlCenter" ;
			if(IaaDebug.Debug) 
				IaaUtil.log( "Looking up Spider Control Center at: " + serverName );
         	try {
				//get the AdSpiderCenter handle
            	adCenter = (SpiderCtl)(Naming.lookup( serverName ));
         	} catch ( Exception e ) {
            	IaaDebug.systemout.println( "Lookup Spider Control Center failed: " + e );
            	SpecialExceptionLog.getInstance().printStackTrace(e);
				System.exit(3);
         	}
			
			return adCenter;

	}

	/**
	  * Lookup the Request Control Center service.
	  *
	  * @params hostName The host name or IP address where the service located
	  * @params registryPort The port listened by the RMI registry ( By default
	  * RMI use port 1099 as the listen port).
	  * 
	  * @return The Request Control Center service object( <code>RequestCtl</code>
	  * object), or null if lookup failed.
	  *
	  * @see RequestCtl
	  * @see RequestCtlImpl
	  * @see IaaServer
	  */	
	public static RequestCtl getRequestCenter(String hostName, int registryPort) 
	{
            RequestCtl reqCenter = null;
			
       		String  serverName = "rmi://" + hostName + ":" + registryPort
         			+ "/RequestCtlCenter" ;
			if(IaaDebug.Debug) 
				IaaUtil.log( "Looking up Request Control Center at: " + serverName );
         	try {
				//get the AdSpiderCenter handle
            	reqCenter = (RequestCtl)(Naming.lookup( serverName ));
         	} catch ( Exception e ) {
	         	IaaDebug.systemout.println( "Lookup Request Control Center failed: " + e );
	         	SpecialExceptionLog.getInstance().printStackTrace(e);
	         	System.exit(3);
         	}
			
			return reqCenter;
	}
	
	/**
	  * Lookup the Spider Database service.
	  *
	  * @params hostName The host name or IP address where the service located
	  * @params registryPort The port listened by the RMI registry ( By default
	  * RMI use port 1099 as the listen port).
	  * 
	  * @return The Spider Database service object( <code>SpiderDBConnection</code>
	  * object), or null if lookup failed.
	  *
	  * @see SpiderDBConnection
	  * @see SpiderDBConnectionImpl
	  * @see IaaServer
	  */	
	public static SpiderDBConnection getSpiderDBConnection(String hostName, int registryPort) 
	{
            SpiderDBConnection sp = null;
			
       		String  serverName = "rmi://" + hostName + ":" + registryPort
         			+ "/SpiderDatabase" ;
			if(IaaDebug.Debug) 
				IaaUtil.log( "Looking up Spider Database Connection at: " + serverName );
         	try {
				//get the AdSpiderCenter handle
            	sp = (SpiderDBConnection)(Naming.lookup( serverName ));
         	} catch ( Exception e ) {
	         	IaaDebug.systemout.println( "Lookup Spider Database failed: " + e );
	         	SpecialExceptionLog.getInstance().printStackTrace(e);
	         	System.exit(3);
         	}
			
			return sp;
	}
	
	public static LogWriter getLogWriter(boolean logWriteToScreen)  {
		if (!logWriteToScreen)  {
			LogWriter tempOut= new LogWriter()  {
				RemoteLogWriter rLog =
						 getLogWriter(IaaDebug.ServerHost,IaaDebug.ServerPort);
			
				//define the println method
				public void println(String x)  {
					try  {
						rLog.println(x);
					} catch (RemoteException e)  {
						System.out.println(x + " :: " + e);
					}
				}//end of println(String)
				public void println(Object x)   {
					println(x.toString());
				}//end of println(Object)
				
				public void printStackTrace(Throwable throwable){
					try  {
						rLog.printStackTrace(throwable);
					} catch (RemoteException e)  {
						System.out.println("Throwable :: " + e);
					}
				}
			}; //end of anonymous class implements the LogWriter interface	
			try {
			   tempOut.println("");
			   return tempOut;
			} catch (NullPointerException e) {
				IaaDebug.systemout.println("Can not find Log Writer, log write to screen.");
				IaaDebug.LogWriteToScreen=true;
			}
		}
		return new LogWriter()  {
			//define the println method
			public void println(String x)  {
				IaaDebug.systemout.println(x);
			}//end of println(String)
			public void println(Object x)   {
				IaaDebug.systemout.println(x);
			}//end of println(Object)
			public void printStackTrace(Throwable throwable){
				throwable.printStackTrace(IaaDebug.systemout);
			}
		}; //end of anonymous class implements the LogWriter interface

	}
	
	public static void printStackTrace(Throwable throwable)
	{
		if(IaaDebug.Debug&&(!IaaDebug.LogWriteToScreen))IaaDebug.say(-1,
				"Exception, detail in log file: " + throwable.toString());
		out.println(IaaThread.getLogString());
		out.printStackTrace(throwable);
	}
	
	public static void log(String s) {
	    s = IaaThread.getLogString() + s;
		out.println(s);
		if(IaaDebug.Debug&&(!IaaDebug.LogWriteToScreen))IaaDebug.say(-1,s);
	}
	public static void auxLog(String s){
	    if(IaaDebug.AuxDebug) log(s) ;
	}
	
	public static void log(Object x)  {
		//stackTrace();
	    String s = IaaThread.getLogString() + x;
		out.println(s);
		if(IaaDebug.Debug&&(!IaaDebug.LogWriteToScreen))IaaDebug.say(-1, s);
	} 
	public static void print(String s) {
		IaaDebug.systemout.println(s);
	}
	
	public static void printlog(String s)  {
		IaaDebug.systemout.println(s);
		if(!IaaDebug.LogWriteToScreen)out.println(s);
	}
	
    private static RemoteLogWriter getLogWriter(String hostName, 
			int registryPort)  {
//System.out.println(hostName+" "+registryPort);
            RemoteLogWriter log = null;

       		String  serverName = "rmi://" + hostName + ":" + registryPort
         			+ "/RemoteLogWriter" ;
			//if(IaaDebug.Debug) 
				//System.out.println( "Looking up LogWriter at: " + serverName );
         	try {
				//get the Log Writer handle
            	log = (RemoteLogWriter)(Naming.lookup( serverName ));
         	} catch ( Exception e ) {
	         	System.out.println( "Lookup LogWriter failed: " + e );
         	}
			
			return log;
	}
	
    //add stack trace to the project by Anthony Zhang 14/Dec/2000
    public static void stackTrace()
    {
        Vector stack = new Vector();
        try
        {
            ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
            PrintStream printstream = new PrintStream(bytearrayoutputstream);
            (new Exception()).printStackTrace(printstream);
            byte abyte0[] = bytearrayoutputstream.toByteArray();
            ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);
            DataInputStream datainputstream = new DataInputStream(bytearrayinputstream);
            printstream.close();
            datainputstream.readLine();
            datainputstream.readLine();
            datainputstream.readLine();
            String s;
            while((s = datainputstream.readLine()) != null) 
                stack.addElement(s);
        }
        catch(IOException _ex)
        {
            return;
        }
	  for (int i = 0;i<stack.size(); i++)  {
		out.println("*****"+stack.get(i));
	  }					
        return;
    }

}